package com.hui.mapper;

import com.hui.dto.AssetsPageDTO;
import com.hui.entity.Assets;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface AssetsMapper {

    @Insert("""
            insert into rms_assets (title, price, price_unit, stock_unit, total) 
            values (#{title}, #{price}, #{priceUnit}, #{stockUnit}, #{total})
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Assets assets);

    @Select("""
            select * from rms_assets t
            where t.id = #{param1} and t.deleted = 0
            """)
    Assets select(Long id);

    @Select("""
            <script>
            select * from rms_assets t
            <where>
            <if test='title != null'> title like concat('%' , #{title}, '%') and </if>
            t.deleted = 0
            </where>
            </script>
            """)
    List<Assets> list(AssetsPageDTO assetsPageDTO);

    @Update("""
            <script>
            update rms_assets
            <set>
            <if test='title != null'> title = #{title},</if>
            <if test='price != null'> price = #{price},</if>
            <if test='priceUnit != null'> price_unit = #{priceUnit},</if>
            <if test='stock != null'> stock = #{stock},</if>
            <if test='stockUnit != null'> stock_unit = #{stockUnit},</if>
            <if test='total != null'> total = #{total},</if>
            <if test='updated != null'> updated = #{updated},</if>
            <if test='deleted != null'> deleted = #{deleted},</if>
            <if test='version != null'> version = #{version},</if>
            version = version + 1
            </set>
            where id = #{id} and deleted = 0 and version = #{version} 
            </script>
            """)
    int update(Assets assets);

    @Update("""
            update rms_assets
            set deleted = 1, updated = current_timestamp
            where id = #{id} 
            """)
    int delete(Long id);

    @Update("""
            <script>
            update rms_assets set deleted = 1, updated = current_timestamp
            where
            <foreach collection='list'  item='e'  open='id in(' close=')' separator=','>
            #{e}
            </foreach>
            </script>
            """)
    int deleteBatch(List<Long> ids);


}
